package com.flyme.module.base.provider.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.*;
import com.flyme.module.base.provider.mapper.SysVipMapper;
import com.flyme.module.base.provider.service.SysUserService;
import com.flyme.module.base.provider.service.SysVipPriceService;
import com.flyme.module.base.provider.service.SysVipService;
import com.flyme.module.base.provider.service.SysVipTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: zyf
 * @date: 2018/10/24 16:33
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysVipServiceImpl extends BaseServiceImpl<SysVipMapper, SysVip> implements SysVipService {

    @Autowired
    private SysVipTypeService vipTypeService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysVipPriceService vipPriceService;

    public static void main(String[] args) {
        //小于0当前日期大于指定时间
        System.out.println(DateUtil.between(DateUtil.date(), DateUtil.parseDateTime("2019-04-11 16:00:06"), DateUnit.MINUTE, false));
        //false过期true正常
        System.out.println(DateUtil.between(DateUtil.parseDateTime("2019-03-17 16:00:06"), DateUtil.date(), DateUnit.SECOND, false));
        Long a = 0L;

        String expiryDate = DateUtil.formatDateTime(DateUtil.offsetMonth(DateUtil.parseDateTime("2018-05-06 12:00:00"), 1));
        System.out.println(expiryDate);
        System.out.println(a.equals(0L));
    }

    /**
     * 添加用户vip
     */
    public boolean addVip(Long userId, Integer month){
        //查询用户当前是否为购买的vip用户，已经购买过的时间增加
        SysVip vipaccount = getOne(q().eq(true, "userId", userId), false);
        SysVipType vipType = vipTypeService.findByCode("VIP");
        if (ObjectUtils.isEmpty(vipaccount)) {//用户没有vip记录
            vipaccount = new SysVip();
            vipaccount.setUserId(userId);
            vipaccount.setVipTypeId(vipType.getId());
            vipaccount.setExpiryDate(DateUtil.formatDateTime(DateUtil.offsetMonth(DateUtil.date(), month)));//当前日期往后推算
        } else {//用户存在vip记录，判断是否为过期（过期从当前算过期，未过期续费时间）
            //小于0当前日期大于过期时间，表示已过期
            Long a = DateUtil.between(DateUtil.date(), DateUtil.parseDateTime(vipaccount.getExpiryDate()), DateUnit.MINUTE, false);
            if (a > 0) {//续费
                String expiryDate = DateUtil.formatDateTime(DateUtil.offsetMonth(DateUtil.parseDateTime(vipaccount.getExpiryDate()), month));
                vipaccount.setExpiryDate(expiryDate);
            }else{//重新添加时间
                vipaccount.setExpiryDate(DateUtil.formatDateTime(DateUtil.offsetMonth(DateUtil.date(), month)));
            }
        }
        boolean tag = saveOrUpdate(vipaccount);
        return tag;
    }

    /**
     * 创建/更新用户vip
     */
    public void addUserVip(Long userId, Long vipPriceId) {

        SysVipPrice vipPrice = vipPriceService.getById(vipPriceId);
        Integer month = vipPrice.getVipTime();
        //查询用户当前是否为购买的vip用户，已经购买过的时间增加
        SysVip vipaccount = getOne(q().eq(true, "userId", userId).eq(true, "vipTypeId", vipPrice.getVipTypeId()), false);
        if (ObjectUtils.isEmpty(vipaccount)) {//用户没有vip记录
            vipaccount = new SysVip();
            vipaccount.setVipTypeId(vipPrice.getVipTypeId());
            vipaccount.setUserId(userId);
            vipaccount.setExpiryDate(DateUtil.formatDateTime(DateUtil.offsetMonth(DateUtil.date(), month)));//当前日期往后推算
        } else {//用户存在vip记录，判断是否为过期（过期从当前算过期，未过期续费时间）
            //如果到期时间大于当前时间，false过期true正常
            Long a = DateUtil.between(DateUtil.date(), DateUtil.parseDateTime(vipaccount.getExpiryDate()), DateUnit.MINUTE, false);
            if (a > 0) {//续费
                String expiryDate = DateUtil.formatDateTime(DateUtil.offsetMonth(DateUtil.parseDateTime(vipaccount.getExpiryDate()), month));
                vipaccount.setExpiryDate(expiryDate);
            }else{//重新添加时间
                vipaccount.setExpiryDate(DateUtil.formatDateTime(DateUtil.offsetMonth(DateUtil.date(), month)));
            }
        }
        saveOrUpdate(vipaccount);
    }

    /**
     * 创建/更新用户vip
     */
    public void addCompVip(Long companyId, Long vipTpeId, String expiryDate) {
        //查询用户当前是否为购买的vip用户，已经购买过的时间增加
        SysVip vip = getOne(q().eq(true, "userId", companyId).eq(true, "vipTypeId", vipTpeId), false);
        if (ObjectUtils.isEmpty(vip)) {//用户没有vip记录
            vip = new SysVip();
            vip.setVipTypeId(vipTpeId);
            vip.setUserId(companyId);
        }
        vip.setExpiryDate(expiryDate);
        saveOrUpdate(vip);
    }

    /**
     * 查询企业vip
     */
    public SysVip getCompanyVip(Long companyId){
        SysVip vip = getOne(q().eq(true, "userId", companyId).eq(true, "vipTypeId", 2), false);
        return vip;
    }

    /**
     * 查询用户vip等级
     */
    public SysVipType getUserVip(Long accountInfoId) {
        SysUser user = sysUserService.getById(accountInfoId);
        SysVip vipaccount = getOne(q().eq(true, "userId", accountInfoId).gt(true, "expiryDate", DateUtil.date()), false);
        if(ObjectUtils.isEmpty(vipaccount)){
            vipaccount = getOne(q().eq(true, "userId", user.getCompanyId()).gt(true, "expiryDate", DateUtil.date()), false);
        }
        SysVipType vip = null;
        if (ObjectUtils.isNotEmpty(vipaccount)) {
            vip = vipTypeService.getById(vipaccount.getVipTypeId());
        }
        return vip;
    }

    /**
     * 查询用户vip等级
     */
    public EntityMap getVipByUser(Long accountInfoId) {
        SysUser accountInfo = sysUserService.getById(accountInfoId);
        EntityMap vip = null;
        SysVip vipaccount = getOne(q().eq(true, "userId", accountInfo.getCompanyId()), false);
        Long a = 1L;
        if(ObjectUtils.isNotEmpty(vipaccount)){//当企业vip不为空的时候，判断是否过期
            a = DateUtil.between(DateUtil.date(), DateUtil.parseDateTime(vipaccount.getExpiryDate()), DateUnit.MINUTE, false);
        }
        if(ObjectUtils.isEmpty(vipaccount) || a < 0){//在不是企业vip情况下或企业vip过期的情况下查询是否为个人vip
            SysVip vipuser = getOne(q().eq(true, "userId", accountInfoId), false);
            if(ObjectUtils.isNotEmpty(vipuser)){
                vipaccount = vipuser;
            }
        }
        if(ObjectUtils.isNotEmpty(vipaccount)){
            vip = new EntityMap();
            SysVipType vipType = vipTypeService.getById(vipaccount.getVipTypeId());
            vip.put("vipCode", vipType.getVipCode());
            vip.put("expiryDate", vipaccount.getExpiryDate());
        }
        return vip;
    }

    /**
     * 查询用户是否为vip（0不是1企业2个人3既是企业又是个人）
     */
    public Integer isVip(Long accountId) {
        Integer isVip = 0;
        SysUser accountInfo = sysUserService.getById(accountId);
        if (ObjectUtils.isNotEmpty(accountInfo)) {
            SysVip company = getOne(q().eq(true, "userId", accountInfo.getCompanyId()).gt(true, "expiryDate", DateUtil.date()), false);
            if (ObjectUtils.isNotEmpty(company)) {//用户所属企业是企业vip
                isVip = 1;
            }
        }
        SysVip user = getOne(q().eq(true, "userId", accountId).gt(true, "expiryDate", DateUtil.date()), false);
        if (ObjectUtils.isNotEmpty(user)) {//用户为个人vip
            if (isVip.equals(1)) {
                isVip = 3;
            } else {
                isVip = 2;
            }
        }
        return isVip;
    }

    /**
     * 根据类型统计vip购买人数
     */
    public Integer countVip(Integer type) {
        return count(q().eq("vipTypeId", type));
    }

}
